package au.com.handsunited.web.controller.project;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.Project;
import au.com.handsunited.model.propertyeditors.JodaDateTimeEditor;
import au.com.handsunited.service.LanguageService;
import au.com.handsunited.service.ProjectService;
import au.com.handsunited.service.ServiceCategoryService;
import au.com.handsunited.service.SkillService;
import au.com.handsunited.utils.constants.DateConstants;
import au.com.handsunited.utils.constants.RegionConstants;
import au.com.handsunited.web.command.project.ProjectCommand;
import au.com.handsunited.web.session.SessionSupport;


/**
 * @author arunc
 *
 */
public abstract class AbstractProjectEditController extends SimpleFormController {

	protected ProjectService projectService;
	protected LanguageService languageService;
	protected SkillService skillService;
	protected ServiceCategoryService serviceCategoryService;
	protected DateConstants dateConstants;
	protected RegionConstants regionConstants;

	private static Logger logger = LoggerFactory.getLogger(AbstractProjectEditController.class);


	/**
	 *
	 */
	public AbstractProjectEditController() {
		setCommandClass(ProjectCommand.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {

		Map<String, Object> data = new HashMap<String, Object>();

        data.put("countries", regionConstants.getCountries());
		data.put("states", regionConstants.getStates());
		data.put("days", dateConstants.getDays());
		data.put("months", dateConstants.getMonths());
		data.put("years", dateConstants.getFutureYears());
		data.put("hours", dateConstants.getHours());
		data.put("minutes", dateConstants.getMinutes());
        data.put("serviceCategories", serviceCategoryService.getAllServiceCategories());
        data.put("languages", languageService.getAllLanguages());
        data.put("skills", skillService.getAllSkills());

		List<BaseEntity> ownersList = new ArrayList<BaseEntity>();
		Map<String, BaseEntity> ownersMap = new HashMap<String, BaseEntity>();
		ownersList.addAll(SessionSupport.getUser(request).getOwnedOrganisations());
		Map<String, String> owners = new HashMap<String, String>();
		for (BaseEntity baseEntity : ownersList) {
			owners.put(baseEntity.getId().toString(), baseEntity.getName());
			ownersMap.put(baseEntity.getId().toString(), baseEntity);
		}
		data.put("owners", owners);

		ProjectCommand projectCommand = (ProjectCommand)command;
		projectCommand.setOwnersMap(ownersMap);

        return data;
	}


	/**
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {

		ProjectCommand command = null;

		if (StringUtils.isNotBlank(request.getParameter("id"))) {
			Long projectId = Long.parseLong(request.getParameter("id"));
			Project project = projectService.getProject(projectId);
			command = new ProjectCommand(project);
		} else {
			command = new ProjectCommand();
		}

		command.setServiceCategoryMap(serviceCategoryService.getServiceCategoryMap());
		command.setLanguageMap(languageService.getLanguagesMap());
		command.setSkillMap(skillService.getSkillsMap());

		return command;
	}


	/**
	 * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder)
	 */
	@Override
	protected void initBinder (HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(DateTime.class, new JodaDateTimeEditor("yyyyMMdd"));
	}


	/**
	 *
	 * @param possibleOwners
	 * @param ownerId
	 * @return
	 */
	protected BaseEntity getOwner(List<BaseEntity> possibleOwners, String ownerId) {

		for (BaseEntity baseEntity : possibleOwners) {
			if (baseEntity.getId().equals(Long.parseLong(ownerId))) {
				return baseEntity;
			}
		}

		return null;
	}


	/**
	 * @param projectService the projectService to set
	 */
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	/**
	 * @param serviceCategoryService the serviceCategoryService to set
	 */
	public void setServiceCategoryService(ServiceCategoryService serviceCategoryService) {
		this.serviceCategoryService = serviceCategoryService;
	}

	/**
	 * @param dateConstants the dateConstants to set
	 */
	public void setDateConstants(DateConstants dateConstants) {
		this.dateConstants = dateConstants;
	}

	/**
	 * @param regionConstants the regionConstants to set
	 */
	public void setRegionConstants(RegionConstants regionConstants) {
		this.regionConstants = regionConstants;
	}

	/**
	 * @param languageService the languageService to set
	 */
	public void setLanguageService(LanguageService languageService) {
		this.languageService = languageService;
	}

	/**
	 * @param skillService the skillService to set
	 */
	public void setSkillService(SkillService skillService) {
		this.skillService = skillService;
	}


}
